package com.huatai.datacenter.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.huatai.common.api.R;
import com.huatai.datacenter.entity.ClusterEntity;
import com.huatai.datacenter.service.ClusterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Lion
 * @date 2023/3/20  10:51
 */
@RequestMapping("/cluster")
@RestController
@Api(value = "集群管理",tags = "集群管理")
public class ClusterController extends BaseController<ClusterService,ClusterEntity> {

	@Autowired
	private ClusterService clusterService;

	@PostMapping("/savaClusterInfo")
	@ResponseBody
	@ApiOperation("创建kafka集群")
	@ApiOperationSupport(includeParameters = {
		"cluster.clusterName",
		"cluster.zkpAddress",
		"cluster.broker",
		"cluster.kafkaVersion"
	})
	public R savaCluster(@RequestBody ClusterEntity cluster){

		if (!clusterService.clusterExits(cluster,false)){
			if(clusterService.save(cluster)){
				return R.success("success");
			}else {
				return R.fail("error");
			}
		}else {
			return R.fail("error");
		}
	}

	@PostMapping("/updateClusterInfo")
	@ResponseBody
	@ApiOperation("更新kafka集群")
	@ApiOperationSupport(includeParameters = {
		"cluster.id",
		"cluster.clusterName",
		"cluster.zkpAddress",
		"cluster.broker",
		"cluster.kafkaVersion"
	})
	public R updateCluster(@RequestBody ClusterEntity cluster){

		if (!clusterService.clusterExits(cluster,true)){
			if(clusterService.updateById(cluster)){
				return R.success("success");
			}else {
				return R.fail("error");
			}
		}else {
			return R.fail("error");
		}
	}

	@RequestMapping("/deleteById")
	@ApiOperation("删除集群")
	@ApiImplicitParam(name = "id", value = "主键id",dataType="long",required=true)
	public R delete(@RequestBody JSONObject data) {

		QueryWrapper<ClusterEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("id",data.get("id"));
		clusterService.remove(queryWrapper);

		return R.success("success");
	}

	/**
	 * @author Lion
	 * @description 在分页获取数据之前先检查并更新所有kafka的状态
	 * @param data
	 * @return com.huatai.common.api.R
	 */
	@Override
	public R page(@RequestBody JSONObject data) {

		List<ClusterEntity> list = this.service.list();
		if (list != null){
			for (int i=0;i<list.size();i++){
				clusterService.getClusterAndStatus(list.get(i));
			}
		}

		IPage page = this.service.pageByRequestMap(data);
		this.service.buildExInfo(page.getRecords());
		return R.data(page);
	}

	@Override
	public R list() {
		QueryWrapper queryWrapper = new QueryWrapper();
		queryWrapper.eq("status",1);
		List<ClusterEntity> dbDatas = this.service.list(queryWrapper);
		this.service.buildExInfo(dbDatas);
		return R.data(dbDatas);
	}




}
